package com.wjw.bms.service.impl;

import java.util.List;

import net.sf.json.JSON;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wjw.bms.dao.HzMenuDao;
import com.wjw.bms.model.HzMenu;
import com.wjw.bms.service.HzMenuService;

@Service
public class HzMenuServiceImpl extends SqlSessionDaoSupport implements HzMenuService {

	@Autowired
	private HzMenuDao hzMenuDao;
	/**
	 * 查询子菜单
	* @Title: getHzMenu 
	* @Description: 查询所有的菜单 
	* @return 返回菜单的集合
	* @see com.wjw.bms.service.HzMenuService#getHzMenu()
	 */
	public List<HzMenu> getHzMenu() {
		List<HzMenu> menuList = hzMenuDao.getHzMenu();
		return menuList;
	}
	/**
	 * 查询父菜单
	* @Title: getHzPreviousMenu 
	* @Description: TODO 
	* @return 
	* @see com.wjw.bms.service.HzMenuService#getHzPreviousMenu()
	 */
	public List<HzMenu> getHzPreviousMenu() {
		List<HzMenu> previousMenuList = hzMenuDao.getHzPreviousMenu();
		return previousMenuList;
	}
	/**
	 * 查询所有菜单,并组建成json格式的树
	* @Title: getHzTreeMenu 
	* @Description: TODO 
	* @return 
	* @see com.wjw.bms.service.HzMenuService#getHzTreeMenu()
	 */
	public JSONObject getHzTreeMenu() {
		String json1 = "";
		List<HzMenu> allMenuList =  hzMenuDao.getHzTreeMenu();
		for(int i=0;i<allMenuList.size();i++){
			String previousid = ""+allMenuList.get(i).getPreviousId();
			String menuid =""+ allMenuList.get(i).getMenuId();
			String menu_id = menuid.substring(0,1);
			if(StringUtils.isNotBlank(previousid)){
				json1 = json1+"{\"menuId\":\""+allMenuList.get(i).getMenuId()+"\","
						+ "\"menuName\":\""+allMenuList.get(i).getMenuName()+"\"}";
			}
			if(previousid != "" && previousid != null && menu_id == previousid){
				json1 = json1+"{\"previousId\":\""+allMenuList.get(i).getPreviousId()+"\","
						+ "\"previousName\":\""+allMenuList.get(i).getPreviousName()+"\",\"childNode\":[";
						json1 = json1+"{\"childId\":\""+allMenuList.get(i).getMenuId()+"\"},";
						json1 = json1+"{\"childName\":\""+allMenuList.get(i).getPreviousName()+"\"}]";
						json1 = json1+"}";
			}
		}
		System.out.println(json1.trim());
		JSONObject myJsonObject = null;
		try {
			myJsonObject = new JSONObject(json1);
			System.out.println(myJsonObject);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return myJsonObject;
	}

}
